package org.springframework.howelly.services;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.binding.message.MessageContext;
import org.springframework.howelly.model.OrdenDePago;
import org.springframework.howelly.validators.OrdenDePagoValidator;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.webflow.execution.RequestContext;

@Service("ordenDePagoService")
@Repository
public class JpaOrdenDePagoService implements OrdenDePagoService {

    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
	this.em = em;
    }

    public boolean createOrdenDePago(OrdenDePago ordenDePago, MessageContext context) {
	if (OrdenDePagoValidator.validateNewOrdenDePago(ordenDePago, context)) {
	    em.persist(ordenDePago);
	    return false;
	}
	return true;
    }

    public List<OrdenDePago> findOrdenesDePago(String username, RequestContext context) {
	return em.createQuery("select op from OrdenDePago op").getResultList();

    }
}
